package com.ls.markt.service.admin;

import com.ls.markt.model.admin.Events;
import com.ls.markt.repository.admin.EventsRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EventsService {

    @Resource
    EventsRepository eventsRepository;

    /**
     * 查询全部
     * @return
     */
    public List<Events> findAll(){
        return eventsRepository.findAll();
    }

    /**
     * 分页 查询全部
     * @param pageRequest
     * @return
     */
    public Page<Events> findAll(PageRequest pageRequest){
        return eventsRepository.findAll(pageRequest);
    }

    public void saveEvents(Events events){
        eventsRepository.save(events);
    }

    public Events getEventsById(Long id){
        return eventsRepository.getOne(id);
    }

    public void removeEvents(Long[] ids){
        for (Long id: ids){
            eventsRepository.deleteById(id);
        }
    }

    /**
     * 根据id查询名称
     * @param id
     * @return
     */
    public String getNameById(Long id){
        return getEventsById(id).getName();
    }

    /**
     * 根据名字获取id
     */
    public Long getIdByName(String name){
        Events events = eventsRepository.findByName(name);
        return events.getId();
    }


}
